﻿@using HardHatCore.ApiModels.Shared.TaskResultTypes;
@using HardHatCore.HardHatC2Client.Pages;

<MudTable T="ProcessItem" @ref=@_table Items="Outputs" HorizontalScrollbar="false" Virtualize="true" FixedHeader="true" Hover="true" Dense="true" Outlined="true" Striped="true" Filter="new Func<ProcessItem,bool>(FilterFunc1)" SortLabel="Sort By">
    <ToolBarContent>
        <MudText Typo="Typo.h6">Process Listing</MudText>
        <MudSpacer />
        <MudTextField @bind-Value="searchString1" Placeholder="Search" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"></MudTextField>
        
    </ToolBarContent>
    <HeaderContent>
        <MudTh Style="@setStyle(Settings.IsCheckedBox)"><MudTableSortLabel SortBy="new Func<List<ProcessItem>, ProcessItem>(x=>x.ElementAt(0))">Process Name</MudTableSortLabel></MudTh>
        <MudTh Style="@setStyle(Settings.IsCheckedBox)"><MudTableSortLabel SortBy="new Func<List<ProcessItem>, ProcessItem>(x=>x.ElementAt(1))">Process Path</MudTableSortLabel></MudTh>
        <MudTh Style="@setStyle(Settings.IsCheckedBox)"><MudTableSortLabel SortBy="new Func<List<ProcessItem>, ProcessItem>(x=>x.ElementAt(2))">Owner</MudTableSortLabel></MudTh>
        <MudTh Style="@setStyle(Settings.IsCheckedBox)"><MudTableSortLabel SortBy="new Func<List<ProcessItem>, ProcessItem>(x=>x.ElementAt(3))">PID</MudTableSortLabel></MudTh>
        <MudTh Style="@setStyle(Settings.IsCheckedBox)"><MudTableSortLabel SortBy="new Func<List<ProcessItem>, ProcessItem>(x=>x.ElementAt(4))">PPID</MudTableSortLabel></MudTh>
        <MudTh Style="@setStyle(Settings.IsCheckedBox)"><MudTableSortLabel SortBy="new Func<List<ProcessItem>, ProcessItem>(x=>x.ElementAt(5))">SessionID</MudTableSortLabel></MudTh>
        <MudTh Style="@setStyle(Settings.IsCheckedBox)"><MudTableSortLabel SortBy="new Func<List<ProcessItem>, ProcessItem>(x=>x.ElementAt(6))">Arch</MudTableSortLabel></MudTh>
    </HeaderContent>
    <RowTemplate>
        <MudTd>@context.ProcessName</MudTd>
        <MudTd>@context.ProcessPath</MudTd>
        <MudTd>@context.Owner</MudTd>
        <MudTd>@context.ProcessId</MudTd>
        <MudTd>@context.ProcessParentId</MudTd>
        <MudTd>@context.SessionId</MudTd>
        <MudTd>@context.Arch</MudTd>
    </RowTemplate>
    <PagerContent>
        <MudTablePager HorizontalAlignment="HorizontalAlignment.Left" HidePageNumber="true" HidePagination="true"/>
        <MudPagination SelectedChanged="PageChanged" Count="@((_table.GetFilteredItemsCount() + _table.RowsPerPage - 1) / _table.RowsPerPage)" Class="pb-3"/>
    </PagerContent>
</MudTable>


@code {

    [Parameter]
    public List<ProcessItem> Outputs {get; set;}
    private MudTable<ProcessItem> _table;
    private string searchString1 = "";

    private string setStyle(bool IsDark)
    {
        if (IsDark)
        {
            return "background:white; color:black;";
        }
        return "background:#333333; color:white;";
    }
    
    
    public void FillContents()
    {
        
    }

    private bool FilterFunc1(ProcessItem search) => FilterFunc(search, searchString1);

    private bool FilterFunc(ProcessItem search, string searchString)
    {
        if (string.IsNullOrWhiteSpace(searchString))
            return true;
        //for each element in the array if it contains searchString then return true
        if (search.ProcessName.ToLower().Contains(searchString.ToLower()))
        {
            return true;
        }
        if (search.ProcessPath.ToLower().Contains(searchString.ToLower()))
        {
            return true;
        }
        if (search.Owner.ToLower().Contains(searchString.ToLower()))
        {
            return true;
        }
        if (search.ProcessId.ToString().ToLower().Contains(searchString.ToLower()))
        {
            return true;
        }
        if (search.ProcessParentId.ToString().ToLower().Contains(searchString.ToLower()))
        {
            return true;
        }
        if (search.SessionId.ToString().ToLower().Contains(searchString.ToLower()))
        {
            return true;
        }
        if (search.Arch.ToLower().Contains(searchString.ToLower()))
        {
            return true;
        }
        return false;
    }

    private void PageChanged(int i)
    {
        _table.NavigateTo(i - 1);
    }

   
    protected override void OnParametersSet()
    {
        FillContents();
    }

}